<?php

namespace App\Transformers;

use League\Fractal\TransformerAbstract;
use App\Models\DormitoryRating;

/**
 * Class DormitoryRatingTransformer.
 *
 * @package namespace App\Transformers;
 */
class DormitoryRatingTransformer extends TransformerAbstract
{
    /**
     * Transform the DormitoryRating entity.
     *
     * @param \App\Models\DormitoryRating $model
     *
     * @return array
     */
    public function transform(DormitoryRating $model)
    {
        return [
            'id'         => (int) $model->id,
            'company' => $model->company,
            'period_id'         => $model->period_id,
            'period_name'         => optional($model->period)->name,
            'dorm_id'         => $model->dorm_id,
            'dorm_code'         => optional($model->dorm)->code,
            'user_id'         => $model->user_id,
            'user_name'         => optional($model->user)->name,
            'category_name'         => $model->dormRatingItem->ratingCategory->category_name ?? '',
            'subitem_id'         => $model->subitem_id,
            'subitem_name'         => optional($model->dormRatingItem)->subitem_name,
            'subitem_score'         => optional($model->dormRatingItem)->subitem_score,
            'score'         => $model->score,
            'score_rate'         => $model->subitem_score > 0 ? round($model->score / $model->subitem_score * 100, 2) : 0,
            'remark'         => $model->remark,
            'main_remark' => $model->dormRatingMain->remark ?? '',
            /* place your other model properties here */

            'created_at' => $model->created_at->format('Y-m-d H:i:s'),
            'updated_at' => $model->updated_at->format('Y-m-d H:i:s'),
        ];
    }
}
